﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Game_Engine
{
    public class InputAxis
    {
        public Keys PositiveKey
        {
            get;
            set;
        }


        public Keys NegativeKey
        {
            get;
            set;
        }

        //Current input value between -1.0 and 1.0
        public float InputValue
        {
            get;
            private set;
        }

        //How quickly the input changes from negative/positive and vice versa
        public float Speed
        {
            get;
            set;
        }

        //How quickly the input tends to 0 when nothing is pressed
        public float Gravity
        {
            get;
            set;
        }

        public InputAxis(Keys p, Keys n, float s, float g)
        {
            PositiveKey = p;
            NegativeKey = n;
            InputValue = 0.0f;
            Speed = s;
            Gravity = g;
        }

        public void Update()
        {
            //This is how we handle input for an InputAxis object -- only within a game Update method, not here... right?
            //Wrong. We have to have class properties which dictate how Pos/Neg keys affect an axis, 
            //the properties of that axis, and the
            //application of them in this Update() method.

            //Our positive key brings us closer to one if it is the only thing pressed
            if(!InputManager.KeyDown(NegativeKey) && InputManager.KeyDown(PositiveKey))
            {
                InputValue += Speed;
            }
            //Our negative key brings us closer to negative one if it is the only thing pressed
            else if (!InputManager.KeyDown(PositiveKey) && InputManager.KeyDown(NegativeKey))
            {
                InputValue -= Speed;
            }
            //Nothing pressed will bring us to 0 -- Both keys pressed will bring us to 0
            else
            {
                if (InputValue < 0)
                {
                    InputValue += Gravity;
                    if (InputValue > 0)
                        InputValue = 0;
                }
                else if (InputValue > 0)
                {
                    InputValue -= Gravity;
                    if (InputValue < 0)
                        InputValue = 0;
                }
            }

            InputValue = MathHelper.Clamp(InputValue, -1.0f, 1.0f);
        }

        public void KillAxes()
        {
            InputValue = 0;
        }
    }
}